#!/usr/bin/env python
# encoding=utf-8
import datetime
import sys
import time

import utils

reload(sys)
sys.setdefaultencoding("utf-8")


def parse_time(time_str):
    try:
        return datetime.datetime(*time.strptime(time_str, "%Y-%m-%d %H:%M:%S")[:6])
    except:
        return datetime.datetime.utcnow()


def run():
    url = 'http://dragonresearchgroup.org/insight/sshpwauth.txt'
    content = utils.get_url_content(url)

    ips = []
    for index, line in enumerate(content.split('\n')):
        if line.strip().startswith('#'):
            continue
        print index, line

        items = line.split('|')
        if len(items) != 5:
            continue

        ips.append({
            '_id': items[2].strip(),
            "sourceUrl": "http://dragonresearchgroup.org/insight/sshpwauth.txt",
            "sourceName": "Dragon research groupSSH bruteforcePublic",
            "detail": items[1].strip(),
            "cause": items[4].strip(),
            "updateTime": parse_time(items[3].strip())
        })
    utils.save2db(ips)
    # for x in ips:
    #     print x


if __name__ == '__main__':
    run()
